package com.yxc.websocketclientdemo.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.yxc.websocketclientdemo.R;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MyAdapter extends BaseAdapter {

    /**
     * 数据集和
     */
    List<Map<String, Object>> list;

    /**
     * 布局反射器（布局文件反射未布局对象）
     */
    LayoutInflater inflater;

    /**
     * 构造器
     */
    public MyAdapter(Context context){
        this.inflater = LayoutInflater.from(context);
    }

    /**
     * 传入数据集合
     */
    public void setList(List<Map<String, Object>> list){
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 更新列表内容
     */
    public void update(){
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View view = inflater.inflate(R.layout.item, null);
        TextView title = view.findViewById(R.id.user_name);

        Map<String, Object> map = list.get(position);
        title.setText((String)map.get("userName"));

        return view;
    }
}